/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package net.sigawe.gateway.db;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import net.sigawe.gateway.Properties.Loader;

/**
 *
 * @author Hendro
 */
public class DBConnection {

    private Connection conn = null;
    private static DBConnection dbConn = null;
    static final String DB_USER = "root";
    static final String DB_PASS = "vertrigo";
    private Loader load = new Loader();
    String DB_URL = "jdbc:mysql://localhost/gateway";//+ load.getFile().getProperty("DB_NAME");
    private DBConnection() {
        System.out.println("Koneksi Database : " + DB_URL + "");// User : " + DB_USER + " Password : " + DB_PASS);

        if (conn == null) {
            try {
                Class.forName("com.mysql.jdbc.Driver").newInstance();
                conn = DriverManager.getConnection(DB_URL, DB_USER, DB_PASS);
            } catch (ClassNotFoundException ex) {
                Logger.getLogger(DBConnection.class.getName()).log(Level.SEVERE, null, ex);
            } catch (InstantiationException ex) {
                Logger.getLogger(DBConnection.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IllegalAccessException ex) {
                Logger.getLogger(DBConnection.class.getName()).log(Level.SEVERE, null, ex);
            } catch (SQLException ex) {
                Logger.getLogger(DBConnection.class.getName()).log(Level.SEVERE, null, ex);
            }

        }
    }

    public static DBConnection getInstance() {
        DBConnection conn = null;
        if (dbConn == null) {
            dbConn = new DBConnection();
            conn = dbConn;
        } else {
            conn = dbConn;
        }
        return conn;
    }

    public void closeAll() throws SQLException {
        conn.close();
    }

    public Connection getCon() throws SQLException {
        return this.conn;
    }
}
